package cn.kai.org.service.impl;

import cn.kai.basic.exception.BusinessException;
import cn.kai.basic.query.BaseQuery;
import cn.kai.basic.util.BaiduAuditUtils;
import cn.kai.basic.util.PageList;
import cn.kai.org.domain.Employee;
import cn.kai.org.domain.Shop;
import cn.kai.org.domain.ShopAuditLog;
import cn.kai.org.mapper.EmployeeMapper;
import cn.kai.org.mapper.ShopMapper;
import cn.kai.org.service.IShopService;
import cn.kai.basic.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author kai
 * @since 2022-06-12
 */
@Service
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService {
    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private EmployeeMapper employeeMapper;

    /**
     * 店铺入驻逻辑
     *  一：参数空校验
     *    。。不能为空
     *  二：合法校验
     *
     *  三：判断是否曾经已经注册过
     *    1. 店铺不能被注册
     *    2.人是否被注册
     *      2.1 注册了但是没有绑定店铺
     *      2.2 注册了并且已经绑定了店铺
     *
     *  四：保存数据
     *    shop
     *    employee  shopId
     * 店铺入驻
     * @param shop
     */
    @Override
    @Transactional
    public void settlement(Shop shop) {
        Employee admin = shop.getAdmin();
        //1：参数空校验,即使前端前端写了校验，我们也需要写自己的后台校验逻辑
        if (StringUtils.isEmpty(admin.getUsername())
                    || StringUtils.isEmpty(admin.getPassword())
                    || StringUtils.isEmpty(admin.getEmail())
                    || StringUtils.isEmpty(admin.getPhone())
                    || StringUtils.isEmpty(admin.getPassword())
                    || StringUtils.isEmpty(shop.getName())
                    || StringUtils.isEmpty(shop.getAddress())){
            throw new BusinessException("请填写完整数据!");
        }
        //2.合法校验
        if (!BaiduAuditUtils.TextCensor(admin.getUsername())
                        ||!BaiduAuditUtils.TextCensor(shop.getName())
                        ||!BaiduAuditUtils.TextCensor(shop.getAddress())){
            throw new BusinessException("请输入合法信息!");
        }
        //3.1 判断店铺是否被注册
        Shop shopTmp = shopMapper.loadByName(shop.getName());
        if (shopTmp != null){
            throw new BusinessException("该店铺已被注册,请直接登录!");
        }
        //3.2.1 注册了并且已经了绑定店铺
        Employee empTmp = employeeMapper.loadByUsername(admin.getUsername());
        if (empTmp != null && empTmp.getShop_id() != null){
            throw new BusinessException("该用户已注册，且已拥有管理店铺，请更换用户！");
        }

        //3.2.2 注册了但是没有绑定店铺
        if(empTmp == null){
            //注意要返回自增ID
            employeeMapper.save(admin);
            empTmp = admin;
        }

        shop.setAdminId(empTmp.getId());
        //注意要返回自增ID
        shopMapper.save(shop);

        empTmp.setShop_id(shop.getId());
        employeeMapper.update(empTmp);

    }

    /**
     * 店铺激活
     * @param shopId
     */
    @Override
    @Transactional
    public void activation(Long shopId) {
        Shop shop = shopMapper.loadById(shopId);
        shop.setState(1);
        shopMapper.update(shop);
    }

    /**
     * 进行照片链接拼接，用于前端页面展示
     * @param query
     * @return
     */
    @Override
    public PageList<Shop> queryPage(BaseQuery query) {
        Long total = shopMapper.queryCount(query);

        List<Shop> rows = shopMapper.queryData(query);
        for (Shop shop : rows) {
            shop.setLogo("http://123.207.27.208"+shop.getLogo());
        }

        return new PageList<Shop>(total,rows);
    }
}
